package com.syl.service.Impl;

import com.syl.entity.Classroom;
import com.syl.entity.Stu;
import com.syl.entity.StudentVO;
import com.syl.mapper.ClassroomMapper;
import com.syl.mapper.StuMapper;
import com.syl.service.StuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname StuServiceImpl
 * @Description
 * @Date 2025/8/15 20:34
 * @Created by SYL
 */

@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private StuMapper stuMapper;
    @Autowired
    private ClassroomMapper classroomMapper;

    @Override
    public Stu findStuByid(int id) {
        return stuMapper.findStuByid(id);
    }

    @Override
    public Stu findStuByidAndName(int id, String name) {
        return stuMapper.findStuByidAndName(id, name);
    }

    @Override
    public ArrayList<Stu> findAllBtu() {
        return stuMapper.findAllBtu();
    }

    @Override
    public ArrayList<Stu> findStuNameAndAge(Map<String, Object> map) {
        return stuMapper.findStuNameAndAge(map);
    }

    @Override
    public int addStu(Stu stu) {
        return stuMapper.addStu(stu);
    }

    @Override
    public int updateStu(Stu stu) {
        return stuMapper.updateStu(stu);
    }

    @Override
    public int deleteById(int id) {
        return stuMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return stuMapper.deleteByIds(ids);
    }

    @Override
    public ArrayList<StudentVO> findAllStuByClass() {
        return stuMapper.findAllStuByClass();
    }

    @Override
    public StudentVO findStuBySubject(int sid) {
        return stuMapper.findStuBySubject(sid);
    }

    @Override
    public StudentVO findStudentAndAllSubjectAndTeacherByStuid(int sid) {
        return stuMapper.findStudentAndAllSubjectAndTeacherByStuid(sid);
    }

    @Override
    public List<StudentVO> findAllStuByClassV2() {
        // 查询所有学生
        ArrayList<Stu> allBtu = stuMapper.findAllBtu();
        ArrayList<StudentVO> studentVOList = new ArrayList<>();

        allBtu.forEach(stu -> {
            //   根据每个学生对象的Cid 来查教室信息
            Classroom classroom = classroomMapper.findClassroomByStuId(stu.getCid());
            //查询出来的信息装进studentVO对象中再添加进list集合中

            StudentVO studentVO = new StudentVO();
            // BeanUtils是spring框架提供的工具类,copyProperties方法用与拷贝对象属性

            //查询出来的信息装进studentVO对象中
            BeanUtils.copyProperties(stu, studentVO);
            studentVO.setClassroom(classroom);
            studentVOList.add(studentVO);


        });

        return studentVOList;
    }

    @Override
    public StudentVO findStuBySubjectV2(int sid) {
        //查询出学术信息
        Stu stuByid = stuMapper.findStuByid(sid);
        System.out.println("stuByid = " + stuByid);

        // 根据sid查询出学生信息
        StudentVO stuBySubject = stuMapper.findStuBySubject(sid);

        System.out.println("控制层");
        System.out.println("stuBySubject = " + stuBySubject);

        BeanUtils.copyProperties(stuByid, stuBySubject);


        return stuBySubject;
    }
}
